from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 导入数据库连接
from SaaSDevelTool.data_mysql_data import db
# 导入分页
from SaaSDevelTool.data_operation import paging_tool_join
class SaaSDataLogQuery(View):
    def get(self,request):
        # 获取企业编号
        company_id = request.GET.get('company_id')
        # 获取当前页数
        page_number = request.GET.get('page_number')
        # 获取每页数量
        page_size = request.GET.get('page_size')
        # 判断企业编号，当前页数，每页数量，是否为空
        if not all([company_id,page_number,page_size]):
            return create_response(code=400,msg='查询失败：企业编号，当前页数，每页数量不能为空')
        # 判断页数和页码是否为数字
        try:
            int(page_number)
            int(page_size)
        except Exception as err:
            return create_response(code=400,msg='当前页数，每页数量必须为数字')
        # 查询日志sql
        query_log_sql = """
            SELECT
                u.user_name,
                l.create_time,
                l.log_message
            FROM
                db_center_rbac.base_info_log_table l
            JOIN
                db_center_rbac.base_info_user_table u
            ON
                l.user_id = u.user_id 
            WHERE
                l.log_message
            LIKE '企业编号:{}-支付api管理%' 
        """.format(company_id)
        data = paging_tool_join(query_log_sql, int(page_number), int(page_size))
        return create_response(code=200,msg='成功',data=data)
